package me.yuzho.study;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class Ch15 {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    private boolean isPrime(int num) {
        if(num < 2) return false;
        
        int sqrtNum = (int)Math.sqrt(num);
        
        for(int i=2; i<=sqrtNum; i++ ) {
            
            if(num % i == 0) return false;
            
        }
        
        return true;
    }
    
    @Test
    public void testCheckPrime() {
        //fail("Not yet implemented");
        
        for(int i=1; i<=100; i++) {
            if(isPrime(i)) System.out.println(i);
        }
    }
    
    @Test
    public void testPrint9x9() {
        
        for(int i=1, j=1; j<=9; i++) {
            System.out.print(String.format("%d * %d = %d \t", i, j, i*j));
            if(i == j) {
                i = 0;
                j++;
                System.out.println();
            }
        }
    }
    
    private boolean isCircleNumber(int num) {
        
        if(num <11) return false;
        
        
        int oldNum = num;
        int newNum = 0;
        
        while (num > 0) {
            
            newNum = newNum * 10 + (num%10);
            num = num / 10;
        }
        
        
        return oldNum == newNum ;
    }
    
    @Test
    public void testCircleNumber() {
        assertTrue(isCircleNumber(11));
        assertTrue(isCircleNumber(121));
        
        for(int i=0; i<=1000; i++) {
            
            if(isCircleNumber(i)) System.out.println(i);
        }
    }

    @Test
    public void testNextDay() {
        Date now = new Date();
        
        System.out.println(new Date(now.getTime() + 24*60*60*1000));
    }
    
    private int outCircle(int total, int k) {
        List<Integer> dataList = new LinkedList<Integer>();
        
        for(int i =0; i < total; i++) {
            dataList.add(new Integer(i+1));
        }
        
        int index = -1;
        while (dataList.size() > 1) {
            index = (index + k) % dataList.size();
            dataList.remove(index--);
        }
        
        return dataList.get(0).intValue();
    }
    
    @Test
    public void testOutCircle() {
        
        assertEquals(11, outCircle(50, 3));
    }
    
    static class Person {
        final String firstName;
        final String lastName;
        
        public Person(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        @Override
        public String toString() {
            return String.format("Person {firstName: %s, lastName: %s}", firstName, lastName);
        }   
    }
    
    private List<Person> people = Arrays.asList(
            new Person("Jane", "Henderson"),
            new Person("Michael", "White"),
            new Person("Henry", "Brighton"),
            new Person("Hannah", "Plowman"),
            new Person("William", "Henderson"));
    
    @Test
    public void testJava7Compare() {
        people.sort(new Comparator<Person>() {

            @Override
            public int compare(Person o1, Person o2) {
                
                int result = o1.lastName.compareTo(o2.lastName);
                
                if(result == 0)
                    result = o1.firstName.compareTo(o2.firstName);
                
                return result;
            }
        });
        
        people.forEach(System.out::println);
    }
    
    @Test
    public void testJava8Compare() {
        Comparator<Person> c = (o1, o2) -> o1.lastName.compareTo(o2.lastName);
        c.thenComparing((o1, o2) -> o1.firstName.compareTo(o2.firstName));
        people.sort(c);
        people.forEach(System.out::println);
    }
    
    

}
